package com.xinyue.center.mqeventhandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xinyue.center.dao.entity.Account;
import com.xinyue.center.dao.entity.GameRole;
import com.xinyue.center.service.IAccountService;
import com.xinyue.fishinghunter.event.messages.CreatePlayerMQEvent;
import com.xinyue.mqsystem.event.GameEventHandler;
import com.xinyue.mqsystem.event.GameEventMapping;

@GameEventHandler
public class PlayerMQEventHandler {
	@Autowired
	private IAccountService accountService;
	private Logger logger = LoggerFactory.getLogger(PlayerMQEventHandler.class);
	@GameEventMapping(CreatePlayerMQEvent.class)
	public void createPlayerNickname(CreatePlayerMQEvent event) {
		String accountId = event.getParams().getAccountId();
		String nickname = event.getParams().getNickname();
		long playerId = event.getParams().getPlayerId();

		Account account = accountService.selectAccount(accountId);
		if (account != null) {
			
			GameRole gameRole = null;
			for(GameRole existRole : account.getGameRoles()) {
				if(existRole.getPlayerId() == playerId) {
					gameRole = existRole;
				}
			}
			if(gameRole == null) {
				gameRole = new GameRole();
				gameRole.setPlayerId(playerId);
				account.getGameRoles().add(gameRole);
			}
			gameRole.setNickname(nickname);
			accountService.updateAccount(account);
		}
		logger.debug("{} playerId:{} 更新昵称：{}",accountId,playerId,nickname);
	}
}
